package com.general.app.wjgl.action;

import java.util.List;
import java.util.UUID;

import com.general.app.wjgl.dao.entity.Survey;
import com.general.app.wjgl.dao.entity.SurveySendModel;
import com.general.app.wjgl.service.ISurveySendService;
import com.general.app.wjgl.service.ISurveyService;
import com.general.app.xtgl.dao.entity.AssessUserModel;
import com.general.app.xtgl.dao.entity.CompanyModel;
import com.general.app.xtgl.service.IAssessUserService;
import com.general.app.xtgl.service.ICompanyService;
import com.general.common.action.BaseAction;
import com.general.common.mail.MailSend;
import com.general.common.mail.impl.JavaxMailServer;
import com.general.common.model.QueryModel;
import com.general.common.security.entity.User;
import com.general.utils.base.Constant;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 问卷发放管理
 * @author Administrator
 *
 */
public class SurveySendAction extends BaseAction implements ModelDriven<SurveySendModel>{

	private static final long serialVersionUID = -5571279048868407604L;
	
	private SurveySendModel model = new SurveySendModel();
	private ISurveySendService surveySendService;
	private ICompanyService companyService;
	private ISurveyService surveyService;
	private IAssessUserService assessUserService;
	private JavaxMailServer mailServer;
	
	/**
	 * 问卷发放管理列表
	 * @return
	 */
	public String surveySendList(){
		if (QUERY.equals(model.getDoType())) {
			QueryModel queryModel = model.getQueryModel();
			try {
				// 单位列表
				if(Constant.TAB_COMPANY.equals(getRequest().getParameter("tab"))){

					queryModel.setItems(surveySendService.getPagedList(model));
					
					// 个人列表
				}else if(Constant.TAB_SINGEL.equals(getRequest().getParameter("tab"))){
					queryModel.setItems(surveySendService.getPagedUserList(model));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			getValueStack().set(DATA, queryModel);
			return DATA;
		}
		return "surveySendList";
	}	
	
	/**
	 * 问卷发放增加绑定面页
	 * @return
	 */
	public String addSurveySend(){

		ValueStack vs = getValueStack();
		List<CompanyModel> companyList = companyService.getPagedList(new CompanyModel());
		vs.set("companyList", companyList);

		AssessUserModel usrModel = new AssessUserModel();
		// 是被评估人
		usrModel.setAssessed_flg(Constant.ASSESSED_FLG_YES);
		
		// 初始化用户关系列表
		List<AssessUserModel> relationList = assessUserService
		.getUserList(usrModel);
		vs.set("relationList", relationList);
		
		Survey survey = new Survey();
		survey.setUsedFlg(Constant.OPEN);
		List<Survey> surveyList = surveyService.getPagedList(survey);
		vs.set("surveyList", surveyList);
		return "addSurveySend";
	}
	
	/**
	 * 保存问卷绑定
	 * @return
	 */
	public String saveSurveySend(){
		try {
			User user = getCurrentUser();
			model.setInsert_nm(user.getUserid());
			model.setUpdate_nm(user.getUserid());
			String sendId = UUID.randomUUID().toString();
			model.setSend_id(sendId);
			boolean result = surveySendService.insert(model);
			String key = result ? "I99001" : "I99002";
			getValueStack().set(DATA, getText(key));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}

	
	/**
	 * 问卷绑定发放
	 * @return
	 */
	public String sendSurveySend(){
		
		String ids = getRequest().getParameter("ids");
		String tab = getRequest().getParameter("tab");
		try {
			int count = surveySendService.sendSurveySend(tab, ids);
			
			if(count>0){
				// 发送邮件（需要优化）
				mailServer.sendMailList(surveySendService.getSendEmailList(tab, ids));

			}
			String message = count>0 ? "发放成功！" : "成功发放记录"+ids.split(",").length+"条,";
			getValueStack().set(DATA, message);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}
	

	/**
	 * 问卷发放结束
	 * @return
	 */
	public String endSurveySend(){
		
		String ids = getRequest().getParameter("ids");
		String tab = getRequest().getParameter("tab");
		try {
			String message = surveySendService.endSurveySend(tab, ids);
			getValueStack().set(DATA, message);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}
	
	/**
	 * 删除问卷绑定
	 * @return
	 */
	public String delSurveySend(){

		String ids = getRequest().getParameter("ids");
		String tab = getRequest().getParameter("tab");
		try {
			String message = surveySendService.delSurveySend(tab, ids);
			getValueStack().set(DATA, message);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}
	
	public SurveySendModel getModel() {
		return model;
	}

	public void setModel(SurveySendModel model) {
		this.model = model;
	}

	public ISurveySendService getSurveySendService() {
		return surveySendService;
	}

	public void setSurveySendService(ISurveySendService surveySendService) {
		this.surveySendService = surveySendService;
	}

	public ICompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(ICompanyService companyService) {
		this.companyService = companyService;
	}

	public ISurveyService getSurveyService() {
		return surveyService;
	}

	public void setSurveyService(ISurveyService surveyService) {
		this.surveyService = surveyService;
	}

	public IAssessUserService getAssessUserService() {
		return assessUserService;
	}

	public void setAssessUserService(IAssessUserService assessUserService) {
		this.assessUserService = assessUserService;
	}

	public JavaxMailServer getMailServer() {
		return mailServer;
	}

	public void setMailServer(JavaxMailServer mailServer) {
		this.mailServer = mailServer;
	}
}
